using UnityEditor;
using UnityEngine;

namespace ZhenG.Tools
{
    public class RenameWindow : EditorWindow
    {
        [MenuItem("Tools/My Tools/重命名")]
        private static void CreateWindow()
        {
            var window = GetWindow<RenameWindow>("重命名");
            window.Show();
        }

        private const float Width = 60f;
        private int _type; //类型，0为选中，1为子集
        private string _name, _prefix, _suffix; //名字，前缀，后缀
        private bool _useNumb = true; //使用编号
        private int _startIndex = 1, _interval = 1; //开始编号，间隔

        private void OnGUI()
        {
            EditorGUILayout.Space();
            _type = GUILayout.Toolbar(_type, new[] { "选择", "子集" });

            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("名字", GUILayout.Width(Width));
            _name = EditorGUILayout.TextField(_name);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("前缀", GUILayout.Width(Width));
            _prefix = EditorGUILayout.TextField(_prefix);
            EditorGUI.indentLevel = 1;
            EditorGUILayout.LabelField("后缀", GUILayout.Width(Width));
            _suffix = EditorGUILayout.TextField(_suffix);
            EditorGUILayout.EndHorizontal();

            EditorGUI.indentLevel = 0;
            EditorGUILayout.Space();
            _useNumb = EditorGUILayout.BeginToggleGroup("编号", _useNumb);
            EditorGUILayout.BeginHorizontal();
            EditorGUI.indentLevel = 1;
            EditorGUILayout.LabelField("开始", GUILayout.Width(Width));
            _startIndex = EditorGUILayout.IntField(_startIndex);
            EditorGUILayout.LabelField("间隔", GUILayout.Width(Width));
            _interval = EditorGUILayout.IntField(_interval);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndToggleGroup();

            EditorGUILayout.Space();
            if (GUILayout.Button("重命名"))
            {
                if (string.IsNullOrEmpty(_prefix) && string.IsNullOrEmpty(_name) && string.IsNullOrEmpty(_suffix))
                    return;
                switch (_type)
                {
                    case 0:
                        RenameSelection(); //重命名选中对象
                        break;
                    case 1:
                        RenameSelectionChild(); //重命名选中物体的子集对象
                        break;
                }
            }
        }

        //重命名选中的物体
        private void RenameSelection()
        {
            GameObject[] objects = Selection.gameObjects;

            for (var i = 0; i < objects.Length; i++)
            {
                GameObject o = objects[i];

                Undo.RecordObject(o, "rename" + o.name);
                string n = string.IsNullOrEmpty(_name)
                    ? $"{_prefix}{o.name}{_suffix}"
                    : $"{_prefix}{_name}{_suffix}";

                if (_useNumb)
                {
                    int value = _startIndex + i * _interval;
                    o.name = n + value;
                    continue;
                }

                o.name = n;
            }
        }

        //重命名选中的物体子集
        private void RenameSelectionChild()
        {
            foreach (var go in Selection.gameObjects)
            {
                for (int i = 0; i < go.transform.childCount; i++)
                {
                    Transform child = go.transform.GetChild(i);
                    Undo.RecordObject(child.gameObject, "rename" + child.name);
                    string n = string.IsNullOrEmpty(_name)
                        ? $"{_prefix}{child.name}{_suffix}"
                        : $"{_prefix}{_name}{_suffix}";
                    if (_useNumb)
                    {
                        int value = _startIndex + i * _interval;
                        child.name = n + value;
                        continue;
                    }

                    child.name = n;
                }
            }
        }
    }
}